package com.sun.app.service.app;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import com.sun.app.service.BaseService;
import com.sun.core.common.PagingList;
import com.sun.dao.CUserMapper;
import com.sun.dao.CUserMsgMapper;
import com.sun.util.AppResult;
import com.sun.vo.CUser;
import com.sun.vo.CUserExample;
import com.sun.vo.CUserMsg;
import com.sun.vo.CUserMsgExample;

public class MessageService extends BaseService {

	@Autowired
	private CUserMsgMapper msgMapper;
	@Autowired
	private CUserMapper userMapper;

	public AppResult addMsgReply(HttpServletRequest request) {
		AppResult r = new AppResult();
		String UID = (String) request.getSession().getAttribute("UID");
		String MsgId = request.getParameter("msg_id");
		System.out.println(MsgId);
		String MsgContent = request.getParameter("msg_content");
		CUserMsg msg = new CUserMsg();
		msg.setReplymsgid(Integer.valueOf(MsgId));
		msg.setFromuid(UID);
		CUserMsgExample example = new CUserMsgExample();
		example.createCriteria().andMsgidEqualTo(Integer.valueOf(MsgId));
		//获得UID  
		msg.setUid(msgMapper.selectByExample(example).get(0).getUid());
		msg.setMsgcontent(MsgContent);
		msg.setDateline(new Date());
		msgMapper.insertSelective(msg);
		r.setStatus(200);
		r.setMsg("success");
		return r;
	}

	private static final String QUERY_MSG_LIST_BY_ID = "SELECT * FROM c_user_msg WHERE UID = ?";

	public List MsgList(String UID) {
		jt.queryForList(QUERY_MSG_LIST_BY_ID, UID);
		return null;
	}

	@Override
	public Integer add(HttpServletRequest request, String ip) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer delete(HttpServletRequest request, String ip) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer update(HttpServletRequest request, String ip) {
		// TODO Auto-generated method stub
		return null;
	}

	private static final String QUERY_MESSAGE_PAGE_LIST = "SELECT m.*,u.USERTRUENAME FROM c_user_msg m ,c_user u WHERE m.FROMUID = u.UID AND m.UID = ? AND m.REPLYMSGID = 0 order by m.dateline desc";
	private static final String QUERY_MESSAGE_REPLY = "SELECT MSGID,UID,MSGCONTENT,DATELINE FROM c_user_msg WHERE REPLYMSGID = ? ORDER BY DATELINE";

	public PagingList getMessagePageList(String UID, HttpServletRequest request) {
		// TODO Auto-generated method stub
		PagingList msgPageList = getPagingList(QUERY_MESSAGE_PAGE_LIST,
				request, UID);
		List msgList = msgPageList.getList();

		ListIterator it = msgList.listIterator();
		while (it.hasNext()) {
			Map map = (Map) it.next();
			Integer msgId = (Integer) map.get("MSGID");
			Integer replyId = (Integer) map.get("REPLYMSGID");
			List replyList = getReplyList(msgId);
			if (replyList.size() > 0)
				map.put("replyList", replyList);
		}
		return msgPageList;
	}

	public List getReplyList(Integer msgId) {
		// 获得回复
		List replyList = new ArrayList();
		try {
			CUserMsgExample example = new CUserMsgExample();
			example.createCriteria().andReplymsgidEqualTo(msgId);
			example.setOrderByClause("dateline desc");
			List<CUserMsg> replyMsgList = msgMapper
					.selectByExampleWithBLOBs(example);
			ListIterator<CUserMsg> replyListIt = replyMsgList.listIterator();
			while (replyListIt.hasNext()) {
				CUserMsg replyMsg = replyListIt.next();
				Map replyMap = new HashMap();
				replyMap.put("msgId", replyMsg.getMsgid());
				replyMap.put("UID", replyMsg.getUid());
				replyMap.put("UNAME", getUserNameFromCuser(replyMsg.getUid()));
				replyMap.put("msgContent", replyMsg.getMsgcontent());
				replyList.add(replyMap);
			}
			System.out.println(replyList);
			return replyList;
		} catch (NumberFormatException e) {
			return replyList;
		}
	}

	// 获得用户姓名
	public String getUserNameFromCuser(String UID) {
		try {
			CUserExample example = new CUserExample();
			example.createCriteria().andUidEqualTo(UID);
			List<CUser> userDb = userMapper.selectByExampleWithBLOBs(example);
			if (!userDb.isEmpty()) {
				CUser cUser = userDb.get(0);
				return cUser.getUsertruename();
			}
			return "";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return "";
		}
	}

}
